<template>
	<view>
		<!-- pages/index/index.wxml -->
		<!-- 头部 -->
		<navber id="head-navber" :navbarData="navbarData" navbarBg="#ffffff"></navber>
		<view class="allList" :style="scrollHeight >= navTop ? 'padding-top:' + goodsNavH + 'px' : ''">
			<!-- 商品-全部 -->
			<goods :isEnd="goodsEnd" :goodsList="dataList"></goods>
		</view>
	</view>
</template>

<script>
	import navber from '@/components/navber/index';
	import goods from '@/components/goods/index';
	
	const app = getApp();
	const Config = require('../../../config/index.js');
	const Api = require('../../../api/index.js');

	export default {
		components: {
			navber,
			goods
		},
		data() {
			return {
				navbarData: {
					title: '商品列表' //顶部标题
					// is_show_bottom: true, //显示背景图
					// bac_img: '/assets/imgs/navbar/navbar_bg_all', //显示背景图片
				},

				//导航栏
				imgUrl: Config.imgUrl,

				//图片url
				loca_address: {
					nation: '',
					//国
					province: '',
					//省
					city: '',
					//市
					district: '',
					//区
					street: '',
					//街道
					street_number: '' //门牌
				},

				//定位地址
				location: {
					lat: '',
					lng: ''
				},

				selects_goods: 'synthesis',

				//商品排序方式
				banner: [],

				//banner
				selects: '0',

				//标签选中值id
				type_id: 0,

				//分类id
				level: 2,

				//分类级别
				imgUrl: Config.imgUrl,

				//图片url
				scrollHeight: 0,

				//滚动距离
				dataList: [],

				//列表数据
				pagenow: 1,

				isMember: false,

				//是否会员
				isIphonex: app.globalData.isIphoneX,

				stickyTop: app.globalData.stickyTop,
				goodsNavH: 0,

				//切换导航栏高度
				navTop: 205,

				goodsEnd: 'start',
				catList: ''
			};
		},
		onReachBottom: function(e) {
			//滚动底部
			this.getDataList();
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onLoad(options) {
			if (options.type) {
				let temp = this.navbarData;
				temp.title = options.name;
				this.setData({
					navbarData: temp,
					selects: options.type,
					type_id: options.type_id
				});
				this.getDataList();
			} // console.log(options);

			if (options.type_id) {
				let temp = this.navbarData;
				temp.title = options.name;
				let level = options.level;
				this.setData({
					navbarData: temp,
					level: level,
					type_id: options.type_id
				});
				this.getDataList();
			}
		},
		onShow: function() {
			this.setData({
				loca_address: app.globalData.addressInfo
			}); // this.pess()
		},
		onPullDownRefresh: function() {
			this.getDataList(true);
			uni.stopPullDownRefresh();
		},
		methods: {
			confirm_keyword: function(e) {
				// 确认搜索
				uni.navigateTo({
					url: '/pages/search/index'
				});
			},

			onMyCart() {
				uni.navigateTo({
					url: '/pages/cart/index'
				});
			},

			onSwitchCity: function() {
				//切换城市
				uni.navigateTo({
					url: '/pages/index/address/index'
				});
			},

			onConfirmKeyword: function(e) {
				// 确认搜索
				uni.navigateTo({
					url: '/pages/search/index'
				});
			},

			upOrCollect(e) {
				let detail = e.detail;
				let index = e.detail['index'];
				let type = e.detail['type']; //点赞收藏

				var json = {
					goods_id: detail['goods_id'],
					type: detail['type'] //类型 1收藏 2点赞
				};
				Api.upOrCollect(json).then((res) => {
					if (res.code == 1) {
						let upData = this.carList;

						if (parseInt(type) == 1) {
							upData[parseInt(index)]['goods_collect_count'] = res.data.count;
							upData[parseInt(index)]['is_collect'] = res.data.is_type;
						} else {
							upData[parseInt(index)]['goods_up_count'] = res.data.count;
							upData[parseInt(index)]['is_up'] = res.data.is_type;
						}

						this.setData({
							catList: upData
						});
					}
				});
			},

			getDataList(is_update=false) {
				if(is_update){
					this.pagenow = 1;
					this.dataList = [];
				}
				//获取数据
				var datas = {
					type: this.selects,
					level: this.level,
					type_id: this.type_id,
					pagenow: this.pagenow,
					location: this.location
				};
				Api.indexList(datas).then((res) => {
					if (res.data.list.length > 0) {
						var data = this.dataList;
						var is_member = res.data.is_member;
						data = data.concat(res.data.list);
						var page = this.pagenow + 1;
						this.setData({
							dataList: data,
							pagenow: page,
							isMember: is_member
						});
					} else {
						this.setData({
							goodsEnd: 'end'
						}); // wx.showToast({
						//   title: '暂无数据',
						//   icon: 'none',
						//   duration: 2000
						// })
					}
				});
			}
		}
	}
</script>

<style>
	@import './index.css';
</style>
